package com.chenfan.mcn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.extension.excel.ExcelUtils;
import com.chenfan.common.extension.validation.groups.Modify;
import com.chenfan.mcn.model.IncomeContract;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.dto.AnnualRebateApprovalAddDTO;
import com.chenfan.mcn.model.dto.AnnualRebateApprovalSearchDTO;
import com.chenfan.mcn.model.dto.AnnualRebateQueryContractDTO;
import com.chenfan.mcn.model.vo.*;
import com.chenfan.mcn.service.AnnualRebateApprovalService;
import com.chenfan.mcn.service.DiscountApprovalService;
import com.chenfan.mcn.service.IncomeContractService;
import com.chenfan.mcn.utils.CommonUtil;
import com.chenfan.privilege.common.config.SearchAuthority;
import com.chenfan.privilege.common.config.UrlAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 客户返点审批单（原年度返点审批单）
 * @Author liuyang
 * @Date 2021/3/2
 * @Time 17:49
 * @Version V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/annual/rebate")
public class AnnualRebateApprovalController {

    @Autowired
    private AnnualRebateApprovalService annualRebateApprovalService;

    @Autowired
    private DiscountApprovalService discountApprovalService;

    @Autowired
    private IncomeContractService incomeContractService;

    /**
     * 客户返点审批单列表
     *
     * @param page      {@link McnPage}
     * @param searchDTO {@link AnnualRebateApprovalSearchDTO}
     * @return {@link Res}
     */
    @GetMapping(value = "/getList", produces = {"application/json;charset=UTF-8"})
    @UrlAuth({"annualRebateApproval_search"})
    public Res<Page<AnnualRebateVO>> getList(@ApiIgnore McnPage<AnnualRebateVO> page, @SearchAuthority AnnualRebateApprovalSearchDTO searchDTO) {
        return Res.ok(annualRebateApprovalService.getList(page, searchDTO));
    }


    /**
     * 修改客户返点审批单状态
     *
     * @param id     {@link Long}
     * @param status {@link Integer}
     * @return {@link Res}
     */
    @GetMapping(value = "/status/change")
    @UrlAuth({"annualRebateApproval_toVoid","annualRebateApproval_withdraw","annualRebateApproval_submit"})
    public Res<Object> changeStatus(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status)) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return annualRebateApprovalService.changeStatus(id, status) ? Res.ok() : Res.failed();
    }


    /**
     * 新增客户返点审批单
     *
     * @param addDTO {@link AnnualRebateApprovalAddDTO}
     * @return {@link Res}
     */
    @PostMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    @UrlAuth("annualRebateApproval_add")
    public Res<Long> add(@RequestBody @Validated AnnualRebateApprovalAddDTO addDTO) {
        return Res.ok(annualRebateApprovalService.saveData(addDTO));
    }

    /**
     * 编辑客户返点审批单
     *
     * @param addDTO {@link AnnualRebateApprovalAddDTO}
     * @return {@link Res}
     */
    @PostMapping(value = "/modify", produces = {"application/json;charset=UTF-8"})
    @UrlAuth("annualRebateApproval_edit")
    public Res<Long> edit(@RequestBody @Validated(Modify.class) AnnualRebateApprovalAddDTO addDTO) {
        return Res.ok(annualRebateApprovalService.saveData(addDTO));
    }


    /**
     * 根据客户名称获取折扣返点审批单号
     *
     * @param customerId {@link Long}
     * @return {@link List}
     */
    @GetMapping(value = "/getDisCountApprovalCode", produces = {"application/json;charset=UTF-8"})
    public Res<List<String>> getDisCountApprovalCode(Long customerId) {
        return Res.ok(discountApprovalService.getDisCountApprovalCode(customerId));
    }


    /**
     * 根据客户获取收入合同编号
     *
     * @param customerId {@link Long}
     * @return {@link List}
     */
    @GetMapping(value = "/getIncomeCodes", produces = {"application/json;charset=UTF-8"})
    public Res<List<IncomeContract>> getIncomeCodes(Long customerId) {
        return Res.ok(incomeContractService.getIncomeCodesByCustomerId(customerId));
    }

    /**
     * 分页查询可关联的收入合同
     *
     * @param queryContractDTO
     * @return {@link List}
     */
    @PostMapping(value = "/queryIncomeContractByPage", produces = {"application/json;charset=UTF-8"})
    public Res<Page<AnnualRebateIncomeContractListVO>> queryIncomeContractByPage(@RequestBody AnnualRebateQueryContractDTO queryContractDTO) {
        Assert.notNull(queryContractDTO.getCustomerId(), "客户ID不能为空！");
        return Res.ok(incomeContractService.queryIncomeContractByPage(queryContractDTO));
    }

    /**
     * 详情
     *
     * @param id {@link Long}
     * @return {@link Res<AnnualRebateInfo>}
     */
    @GetMapping(value = "/info", produces = {"application/json;charset=UTF-8"})
    public Res<AnnualRebateInfo> info(Long id) {
        return Res.ok(annualRebateApprovalService.info(id));
    }


    /**
     * 根据折扣返点审批单号获取年度优惠政策信息
     *
     * @param discountCode            {@link String}
     * @param accumulatedIncomeAmount {@link BigDecimal}
     * @return {@link AnnualPreferentialPolicyInformation}
     */
    @GetMapping(value = "/policyInformation", produces = {"application/json;charset=UTF-8"})
    public Res<AnnualPreferentialPolicyInformation> getAnnualPreferentialPolicyInformation(String discountCode, BigDecimal accumulatedIncomeAmount) {
        return Res.ok(annualRebateApprovalService.getAnnualPreferentialPolicyInformation(discountCode, accumulatedIncomeAmount));
    }


    /**
     * 客户返点审批单列表导出
     *
     * @param searchDTO {@link AnnualRebateApprovalSearchDTO}
     * @return {@link Res}
     */
    @GetMapping(value = "/exportExcel")
    @UrlAuth({"annualRebateApproval_export"})
    public Res<Object> exportExcel(@SearchAuthority AnnualRebateApprovalSearchDTO searchDTO, @ApiIgnore HttpServletResponse response) {
        annualRebateApprovalService.exportExcel(searchDTO, response);
        return Res.ok();
    }


    /**
     * 计算金额
     *
     * @param discountCode {@link String}
     * @return {@link AnnualCalculateAmount}
     */
    @GetMapping(value = "/calculate/amount")
    public Res<AnnualCalculateAmount> calculateAmount(String discountCode) {
        return Res.ok(annualRebateApprovalService.calculateAmount(discountCode));
    }

    /**
     * 获取收入合同金额
     *
     * @param incomeCode {@link String}
     * @return {@link BigDecimal}
     */
    @GetMapping(value = "/getIncomeContractAmount")
    public Res<BigDecimal> getIncomeContractAmount(String incomeCode) {
        return Res.ok(incomeContractService.getIncomeContractAmount(incomeCode));
    }


    /**
     * 根据收入合同编号获取金额
     *
     * @param incomeCodes {@link List<String>}
     * @return {@link BigDecimal}
     */
    @GetMapping(value = "/getAmountOfMoney")
    public Res<BigDecimal> getAmountOfMoney(@RequestParam("incomeCodes") List<String> incomeCodes) {
        return Res.ok(incomeContractService.getAmountOfMoney(incomeCodes));
    }

    /**
     * 客户返点审批单详情-执行单明细导出
     *
     * @param id 客户返点审批单id
     * @return {@link Res}
     */
    @UrlAuth({"annualRebateApproval_exportFun"})
    @GetMapping(value = "/exportDetail")
    public Res<Object> exportDetail(@RequestParam Long id, @ApiIgnore HttpServletResponse response) {
        List<AnnualRebateExcutionOrderVO> excutionOrderList = annualRebateApprovalService.exportDetail(id);
        ExcelUtils.exportExcel(CommonUtil.defaultList(excutionOrderList), "客户返点审批单-执行单明细", AnnualRebateExcutionOrderVO.class, "客户返点审批单-执行单明细.xlsx", response);
        return Res.ok();
    }
}